/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.MakeTypeDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.model.MakeType;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class MakeTypeDaoImpl extends BaseDaoImpl implements MakeTypeDao {
	public List getList(boolean justEnabled) {
		String sql = "from com.minierp.model.MakeType c";
		if (justEnabled) {
			sql = sql + " where c.enabled=\'Y\'";
		}

		sql = sql + " order by c.sequCode,c.tnSpell";
		return this.getHibernateTemplate().find(sql);
	}

	public List getList(long parentId, boolean justEnabled) {
		String sql = "from com.minierp.model.MakeType c where c.parentId="
				+ parentId;
		if (justEnabled) {
			sql = sql + " and c.enabled=\'Y\'";
		}

		sql = sql + " order by c.sequCode,c.tnSpell";
		return this.getHibernateTemplate().find(sql);
	}

	public int unenabledTree(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.MakeType set enabled=\'N\' where id=:id or parentIdStr like :parentIdStr";
						String parentIdStr = "%;" + id + ";%";
						return new Integer(session.createQuery(hql)
								.setLong("id", id)
								.setString("parentIdStr", parentIdStr)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List getAllChildren(long parentId) {
		String sql = "from com.minierp.model.MakeType as obj where obj.parentIdStr like :parentIdStr order by obj.id";
		String parentIdStr = "%;" + parentId + ";%";
		String[] paramNames = new String[]{"parentIdStr"};
		Object[] values = new Object[]{parentIdStr};
		return this.getHibernateTemplate().findByNamedParam(sql, paramNames,
				values);
	}

	public List getList(String searchKey) {
		String sql = "from com.minierp.model.MakeType c where c.enabled=\'Y\'";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			sql = sql
					+ " and ((c.typeName like :searchKey) or (c.tnSpell like :searchKey) or(c.sequCode like :searchKey))";
		}

		sql = sql + " order by c.sequCode,c.tnSpell";
		Session sess = null;

		List arg5;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("searchKey", "%" + searchKey + "%");
			}

			arg5 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg5;
	}

	public MakeType getByName(String typeName, boolean justEnabled) {
		String sql = "from com.minierp.model.MakeType as obj where obj.typeName=:typeName";
		if (justEnabled) {
			sql = sql + " and obj.enabled=\'Y\'";
		}

		String[] paramNames = new String[]{"typeName"};
		Object[] values = new Object[]{typeName};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (MakeType) list.get(0);
	}
}